<!--
*  2021-03-15
*  成品盘库列表
-->
<template>
  <div id="app" class="product-store">
    <!-- 头部导航 -->
    <van-nav-bar
      ref="header"
      left-arrow
      @click-left="onClickLeft"
      left-text="返回"
    >
      <template #title>
        <span @click="onClickPrev">前一天</span>
        <span @click="show = true" style="margin: 0 10px;">{{ dateTime }}</span>
        <span @click="onClickNext">后一天</span>
      </template>
      <template #right>
        <span @click="onClickRight"><van-icon name="plus" size="20" /></span>
      </template>
    </van-nav-bar>
    <!-- 筛选 -->
    <van-search v-model="designNo" placeholder="请输入搜索关键词" />
    <!-- 总计 -->
    <van-cell
      class="product-store__total"
      v-show="tableData.length !== 0"
      title="总数"
      :value="totalNum"
    >
    </van-cell>
    <!-- 滚动 -->
    <better-scroll
      class="public-scroll"
      :data="tableData"
      :totalPage="totalPage"
      :pulldown="true"
      @pulldown="onGetProductStoreCheck"
    >
      <van-cell-group v-show="tableData.length !== 0" v-for="(data, index) in tableData" :key="index"
      style="margin-bottom: 10px;">
        <van-cell
            :title="data.designNo"
            :label="data.color"
            :value="data.num"
            class="start-cell"
            @click="onClickCheck(data)"
          >
          </van-cell>
      </van-cell-group>
      <van-empty
        class="custom-image"
        image="https://img01.yzcdn.cn/vant/custom-empty-image.png"
        v-show="tableData.length === 0"
        description="暂无数据"
      />
    </better-scroll>

    <!-- 日期筛选 -->
    <van-calendar
      v-model="show"
      :min-date="minDate"
      :max-date="maxDate"
      :round="false"
      @confirm="onConfirm"
    />
  </div>
</template>

<script>
import { openWin, comeBack, getFloat } from "@/utils/index";
import { getProductStoreList, getProductStoreCheckDetail } from "@/api/product";

import dayjs from "dayjs";

export default {
  name: "productStore",
  data() {
    return {
      title: "",
      currentPage: 1,
      totalPage: 1,
      tableData: [],
      pageNum: 1,
      pageSize: 1000,
      show: false,
      designNo: "",
      dateTime: dayjs().format("YYYY-MM-DD"),
      minDate: new Date(dayjs().subtract(1, "year")),
      maxDate: new Date(),
      beginAt: dayjs().format("YYYY-MM-DD 00:00:00"),
      endAt: dayjs().format("YYYY-MM-DD 24:00:00"),
      totalNum: 0,
    };
  },
  watch: {
    // 监听事件筛选
    dateTime() {
      this.onGetProductStoreCheck();
    },
    // 监听designNo
    designNo(newVal, oldVal) {
      // 防止input连续输入时，频繁发请求
      clearTimeout(this.timeout);
      this.timeout = setTimeout(() => {
        // 异步请求
        if (oldVal !== newVal) {
          this.onGetProductStoreCheck();
        }
      }, 1000);
    },
  },
  mounted() {
    const that = this;
    if (process.env.NODE_ENV === "production") {
      this.$nextTick(() => {
        that.initFn(that);
      });
    } else {
      window.apiready = function () {
        that.initFn(that);
      };
    }
  },
  methods: {
    getFloat,
    //初始化
    initFn(that) {
      that.onGetProductStoreCheck();
    },
    // 返回
    onClickLeft() {
      comeBack();
    },
    // 前一天
    onClickPrev() {
      this.dateTime = dayjs(this.dateTime)
        .subtract(1, "day")
        .format("YYYY-MM-DD");
    },
    // 后一天
    onClickNext() {
      if (this.dateTime === dayjs().format("YYYY-MM-DD")) {
        this.$toast.fail("明天还没到");
        return false;
      }
      this.dateTime = dayjs(this.dateTime).add(1, "day").format("YYYY-MM-DD");
    },
    // 时间筛选
    onConfirm(val) {
      this.dateTime = dayjs(val).format("YYYY-MM-DD");
      this.show = false;
    },
    // 新增
    onClickRight() {
      openWin("product-storeEdit");
    },
    // 刷新
    onPulldown() {
      this.onGetProductStoreCheck();
    },
    // 盘库列表 // 下拉刷新
    onGetProductStoreCheck() {
      this.tableData = [];
      this.totalNum = 0;
      let params = {};
      params.pageNum = this.pageNum;
      params.pageSize = this.pageSize;
      params.keyLike = this.designNo;
      if (this.designNo === "") {
        params.beginAt = dayjs(this.dateTime).format("YYYY-MM-DD 00:00:00");
        params.endAt = dayjs(this.dateTime)
          .add(1, "day")
          .format("YYYY-MM-DD 00:00:00");
      }
      getProductStoreList(params, (res) => {
        res.list.forEach((item) => {
          this.totalNum = getFloat(this.totalNum + Number(item.num), 0);
        });
        this.tableData = res.list;
      });
    },
    // 查看详情
    onClickCheck(data) {
      getProductStoreCheckDetail({
        checkId: data.id
      }, res => {
        openWin("product-storeEdit", res);
      })
    },
  },
};
</script>

<style lang="scss">
.product-store {
  .van-search {
    position: relative;
    z-index: 999;
  }
  .van-nav-bar__title {
    display: flex;
    height: 100%;
    align-items: center;
    span {
      display: flex;
      height: 100%;
      align-items: center;
      justify-content: center;
      flex: 1;
      font-size: 14px;
      &:first-child {
        color: #1989fa;
      }
      &:last-child {
        color: #ee0a24;
      }
    }
  }
  .van-nav-bar__right {
    span {
      display: flex;
      height: 100%;
      align-items: center;
    }
  }
  .product-store__total {
    position: relative;
    z-index: 999;
    .van-cell__title {
      flex: 0 0 40%;
    }
    .van-cell__value {
      & > div {
        width: 100%;
        display: flex;
        justify-content: space-between;
      }
    }
    span {
      color: #1989fa;
      font-size: 16px;
    }
  }
  .public-scroll {
    height: calc(100vh - 174px);
    overflow: hidden;
    background: rgba(245, 245, 245, 1);
    .van-cell {
      &:not(:nth-child(1)) {
        padding-top: 0;

        &:last-child {
          border-bottom: 1px solid #ddd !important;
        }

        .van-cell__value {
          flex: 0 0 70%;
          div {
            display: flex;
            flex-wrap: wrap;
            justify-content: flex-end;
            span {
              display: flex;
              flex: 1;
              justify-content: center;
              &:last-child {
                justify-content: flex-end;
              }
            }
          }
        }
      }
      &.start-cell {
        &.van-cell:not(:last-child)::after {
          border-bottom: none;
        }
        .van-cell__title {
          font-weight: 600;
          color: #e4393c;
        }
        .van-cell__label {
          font-weight: 600;
          color: #e4393c;
        }
        .van-cell__value {
          flex: 0 0 45%;
          div {
            display: flex;
            flex-wrap: wrap;
            justify-content: flex-end;
            span {
              display: flex;
              flex: 0 0 50%;
              justify-content: flex-end;
              &.active {
                color: #e4393c;
              }
            }
          }
        }
      }
    }
  }
}
</style>

